Skip to content

Clean up delay functions#1766

Merged
martinling merged 1 commit into
greatscottgadgets:mainfrom
martinling:delay-cleanup
Jun 9, 2026
Merged

Clean up delay functions#1766
martinling merged 1 commit into
greatscottgadgets:mainfrom
martinling:delay-cleanup

Conversation

@martinling

Copy link
Copy Markdown
Member

We had a rather scattered selection of delay functions being used:

  • delay simply took a number of loop iterations, for a C loop with a nop in it. Analysis of this loop as compiled by GCC 15.2 shows it currently takes 7 cycles per iteration on the M4. This function has been removed, and all uses of it have been replaced with calls to delay_us or delay_ms according to either apparent intent, or actual effect, at each call site.
00004618 <delay>:
    4618:       2300            movs    r3, #0
    461a:       4283            cmp     r3, r0           // 1 cycle
    461c:       d100            bne.n   4620 <delay+0x8> // 2 cycles, branch taken
    461e:       4770            bx      lr               // skipped
    4620:       bf00            nop                      // 1 cycle
    4622:       3301            adds    r3, #1           // 1 cycle
    4624:       e7f9            b.n     461a <delay+0x2> // 2 cycles, branch taken

  • delayms in rad1o/display.c, which builds on delay but "naively assumes 204 MHz instruction cycle clock and five instructions per count" (wrong, though might have been right on earlier compilers). Replaced with delay_ms.
  • portapack_sleep_milliseconds, which did the same. Replaced with delay_ms.
  • delay_us_at_mhz, which I added to provide an actually-calibrated loop, but which requires knowing the current CPU clock speed. Replaced with delay_us, which uses delay_us_at_mhz internally, but with the clock speed parameter provided by the new cpu_clock_mhz symbol, updated by code in cpu_clock when making changes.
  • halt_and_flash passed its argument to delay. Replaced that call with delay_ms, and updated arguments to halt_and_flash accordingly.

@martinling martinling marked this pull request as draft May 29, 2026 08:31
@martinling martinling marked this pull request as ready for review June 2, 2026 15:57
@martinling martinling requested a review from miek June 4, 2026 15:40
@martinling martinling added this to the harmony milestone Jun 9, 2026

@mndza mndza left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@martinling martinling merged commit fd470d0 into greatscottgadgets:main Jun 9, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants